
TARGET = testtool
OBJDIR = build

CROSS_COMPILE=aarch64-buildroot-linux-gnu-#arm-buildroot-linux-uclibcgnueabihf-#aarch64-buildroot-linux-gnu-
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)gcc
STRIP=$(CROSS_COMPILE)strip

INCDIR += -I./cantool \
            -I./serial
LIBDIR += 

OBJDIR = build

CFLAGS += -g -Wall -O9
CFLAGS += $(INCDIR)

LDFLAGS += -g -O9
LDFLAGS += $(LIBDIR)
LDFLAGS += #-lallegro_encode -lallegro_decode -lmodbus
LDFLAGS += #-lavdevice -lavformat -lavcodec -lswresample -lswscale -lavutil
LDFLAGS += #-lm -lpthread  -ldrm

vpath %.c cantool serial
MAIN_SRCS := main.c 
CAN_SRCS  := cantool/cantool.c
UART_SRCS := serial/serial.c

SRCS = $(MAIN_SRCS) $(CAN_SRCS) $(UART_SRCS)
OBJS = $(patsubst %.c, $(OBJDIR)/%.o, $(notdir $(SRCS)))
                                                                                                                        
all:dir_create $(TARGET)

$(TARGET):$(OBJS)
	$(LD) $(OBJS) -o $(TARGET)_g $(LDFLAGS) 
	$(STRIP) --strip-unneeded -o $(TARGET) $(TARGET)_g

define CREATE_DIR
	if [ ! -d $(1) ];\
	 	then\
    	mkdir -p $(1);\
	fi	
endef

dir_create:  
	@$(call CREATE_DIR,$(OBJDIR))

clean:
	rm -rf $(OBJDIR)/*
	rm -f $(TARGET)_g $(TARGET)

$(OBJDIR)/%.o:%.S
	@echo "CC $<"
	@$(CC) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.c
	@echo "CC $<"
	@$(CC) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.d:%.c
	@set -e; rm -f $@; $(CC) -MM $< $(CFLAGS) > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

-include $(OBJS:.o=.d)


